#include "app_frame.h"
#include "font_manager.h"
#include "../core/audio/audio.h"

KAppFrame::KAppFrame()
{
	mFPS = 0;
	mTimeTicks = 0;
	mTimeSinceLast = 0;
	mFrameSinceLast = 0;
	mBK_Color = 0xff00005f;
}

KAppFrame::~KAppFrame()
{

}

bool KAppFrame::Init()
{
	if (!KFontManager::GetInstance()->Initialize())
		return false;
	
	if (!KAudio::Initialize())
		return false;

	// Calculate the initial state of canvas & view
	// default canvas size
	mCanvasViewState.width = 800;
	mCanvasViewState.height = 480;
	mCanvasViewState.viewCenter[0] = (float)mCanvasViewState.width * 0.5f;
	mCanvasViewState.viewCenter[1] = (float)mCanvasViewState.height * 0.5f;
	mCanvasViewState.viewZoom = 1.0f;
	ValidateCanvasViewSetting(mCenterRectLimit);	

	unsigned int viewWidth, viewHeight;
	KDrawManager::GetViewportSize(viewWidth, viewHeight);
	mCanvasViewState.viewCenter[0] = (float)mCanvasViewState.width * 0.5f;
	mCanvasViewState.viewCenter[1] = (float)mCanvasViewState.height * 0.5f;
	//float ratioW = (float)mCanvasViewState.width / (float)viewWidth;
	//float ratioH = (float)mCanvasViewState.height / (float)viewHeight;

	mCanvasViewState.viewZoom = 1.0f;//std::max(ratioW, ratioH);

	return true;
}

void KAppFrame::Destory()
{
	KAudio::Destory();
	KFontManager::GetInstance()->Destory();
}

void KAppFrame::ValidateCanvasViewSetting(KRect& centerRectLimit)
{
	// Calculate the view center limitation
	unsigned int viewWidth, viewHeight;
	KDrawManager::GetViewportSize(viewWidth, viewHeight);
	float vViewWidth = (float)viewWidth * mCanvasViewState.viewZoom;
	float vViewHeight = (float)viewHeight * mCanvasViewState.viewZoom;

	if (vViewWidth > (float)mCanvasViewState.width) {
		mCanvasViewState.viewCenter[0] = (float)mCanvasViewState.width * 0.5f;
		centerRectLimit.left_bottom[0] = mCanvasViewState.viewCenter[0];
		centerRectLimit.right_top[0] = mCanvasViewState.viewCenter[0] + 1;
	}
	else {
		float leftLimit = mCanvasViewState.viewCenter[0] - (float)vViewWidth * 0.5f;
		float rightLimit = mCanvasViewState.viewCenter[0] + (float)vViewWidth * 0.5f;
		if (leftLimit < 0) mCanvasViewState.viewCenter[0] -= leftLimit;
		if (rightLimit > (float)mCanvasViewState.width) mCanvasViewState.viewCenter[0] -= (rightLimit - (float)mCanvasViewState.width);

		centerRectLimit.left_bottom[0] = vViewWidth*0.5f;
		centerRectLimit.right_top[0] = (float)mCanvasViewState.width - vViewWidth*0.5f;
	}

	if (vViewHeight > (float)mCanvasViewState.height) {
		mCanvasViewState.viewCenter[1] = (float)mCanvasViewState.height * 0.5f;
		centerRectLimit.left_bottom[1] = mCanvasViewState.viewCenter[1];
		centerRectLimit.right_top[1] = mCanvasViewState.viewCenter[1] + 1;
	}
	else {
		float bottomLimit = mCanvasViewState.viewCenter[1] - vViewHeight * 0.5f;
		float topLimit = mCanvasViewState.viewCenter[1] + vViewHeight * 0.5f;
		if (bottomLimit < 0) mCanvasViewState.viewCenter[1] -= bottomLimit;
		if (topLimit > (float)mCanvasViewState.height) mCanvasViewState.viewCenter[1] -= (topLimit - (float)mCanvasViewState.width);

		centerRectLimit.left_bottom[1] = vViewHeight*0.5f;
		centerRectLimit.right_top[1] = (float)mCanvasViewState.height - vViewHeight*0.5f;
	}

	mVirtualViewportSize[0] = vViewWidth;
	mVirtualViewportSize[1] = vViewHeight;
	mViewportCenter = mCanvasViewState.viewCenter;
	KDrawManager::SetupView(vViewWidth, vViewHeight, mCanvasViewState.viewCenter, mBK_Color);
}

void KAppFrame::EnableCanvasView(bool ena)
{
	if (ena)
		KDrawManager::SetupView(mVirtualViewportSize[0], mVirtualViewportSize[1], mViewportCenter, mBK_Color);
	else {
		unsigned int viewWidth, viewHeight;
		KDrawManager::GetViewportSize(viewWidth, viewHeight);
		KVec2 center((float)viewWidth*0.5f, (float)viewHeight*0.5f);
		KDrawManager::SetupView((float)viewWidth, (float)viewHeight, center, mBK_Color);
	}
}

void KAppFrame::OnChangeViewPerFrame(CanvasViewState& viewState, const KRect& centerRectLimit)
{

}

void KAppFrame::TimeElapse(unsigned int ms)
{
	OnChangeViewPerFrame(mCanvasViewState, mCenterRectLimit);
	ValidateCanvasViewSetting(mCenterRectLimit);	
	EnableCanvasView(true);

	if (Render())
		++mFrameSinceLast;

	KAudio::TimeElapse(ms);

	mTimeTicks += ms;
	mTimeSinceLast += ms;
	if (mTimeSinceLast > 500) {
		mFPS = (float)mFrameSinceLast / (float)mTimeSinceLast * 1000.0f;
		mFrameSinceLast = 0;
		mTimeSinceLast = 0;
	}
}


bool InitApp()
{
	KAppFrame::GetInstance()->Init();
	return true;
}


void DestoryApp()
{
	KAppFrame::GetInstance()->Destory();
}



bool TimeElapse(double sec)
{
	KAppFrame::GetInstance()->TimeElapse((unsigned int)(sec * 1000.0));
	return true;
}